$(function(){
    new Vue({
        el: "#right",
        data:{
            form_data:{
                module_name: "心脑血管",
                title: "",                           //标题名称
                description: "",                     //描述
                keyword: "",
                source: "",                          //来源作者或者机构
                type: 1,                             //1.网站内容,2广告
                content: "",                         //内容
                style: 1,                            //样式
                state: false,                        //是否发布  false 0 未发布  true 1 已发布  2 取消发布
                photoUrl: ["","",""]                         //图片数组
            },
            isIE:'',
            rootPath:'http://localhost:3000/'
        },
        ready:function() {
            this.isIE = /msie/i.test(navigator.userAgent) && !window.opera;
        },
        methods: {
            save: function () {


            },
            clear: function(element) {
                var count = element.split("_")[2] - 1;
                var self = this;
                var img = $('.img_address').eq(count).find(".thumb_img").find("img");
                var span = $('.img_address').eq(count).find(".thumb_img").find("span");
                if(img.attr('src').replace(self.rootPath,"") != "img/favicon.png") {
                    $.ajax({
                        url: self.rootPath+"app/img/delete?img_url="+img.attr('src').replace(self.rootPath,""),
                        type: "get",
                        dataType: "json",
                        success: function (data) {
                            img.attr('src','img/favicon.png');
                            span.text("无图片");
                            $('#'+element).val("");
                            self.form_data.photoUrl[count] = "";
                            alert(data.msg);
                        }
                    });
                }
            },
            img_select: function(element) {
                $('#'+element).click();
            },
            img_change: function(e) {
                e = e || window.event;
                var obj = e.target || e.srcElement;
                var imgSize = 0;
                var reader = new FileReader();
                if (this.isIE && !obj.files) {
                    var filePath = obj.value;
                    var fileSystem = new ActiveXObject("Scripting.FileSystemObject");
                    var file = fileSystem.GetFile(filePath);
                    imgSize = file.Size;
                } else {
                    imgSize = obj.files[0].size;
                }
                if ((imgSize / 1024) > 2000) {
                    alert("附件不能大于2M");
                    obj.value = "";
                    return  false;
                }
                var name = obj.value;
                var fileName = name.substring(name.lastIndexOf(".") + 1).toLowerCase();
                if (fileName != "jpg" && fileName != "jpeg" && fileName != "pdf" && fileName != "png" ) {
                    alert("请选择图片格式文件上传(jpg,png,gif,gif等)！");
                    obj.value = "";
                    return
                }
                reader.readAsDataURL(obj.files[0]);
                reader.onload = function (event) {
                    obj.parentElement.previousElementSibling.querySelector("img").src = reader.result;
                    obj.parentElement.previousElementSibling.querySelector("span").innerHTML = name;
                };
            },
            img_upload: function(fileElementId) {
                var self = this;
                var count = fileElementId.split("_")[2] - 1;
                if($('#'+fileElementId).val() == ""){
                    alert("上传图片不能为空!!!!");
                    return ;
                }
                $.ajaxFileUpload({
                    url: self.rootPath+'app/img?module=app_content&name='+self.form_data.module_name+'&type='
                            +self.form_data.type+'&style='+self.form_data.style+'&img_id='+fileElementId+"&img_url="+self.form_data.photoUrl[count],
                    secureuri: false,
                    fileElementId: fileElementId,
                    type:'post',
                    success: function(data) {
                        var dataStr = $(data).find("body").text();//获取返回的字符串
                        var dataJson = $.parseJSON(dataStr);//把字符串转化为json对象
                        if(dataJson.status) {
                            self.form_data.photoUrl[count] = dataJson.data.path.replace(/\\/g, "/");
                            $('.img_address').eq(count).find(".thumb_img").find("img").attr('src',self.rootPath+self.form_data.photoUrl[count]);
                            $('.img_address').eq(count).find(".thumb_img").find("span").text(dataJson.data.name);
                            $('#'+fileElementId).change(self.img_change);
                            alert("上传成功!!!");
                        } else {
                            alert(dataJson.error);
                        }
                    }
                });
            }
        }
    });
});

